#
import os
import webview
from pickle import TRUE


# 配置pywebview关闭提示的中文翻译
import lib.helpers
from lib import helpers, until, spiderbd_v3, material 
from lib import config
from lib.email import sendEmail
from lib.watchlogs import watchlogs, writeNull

chinese = {
    "global.quitConfirmation": u"确定关闭?",
}


# 传给前端的api对象, 定义了一个可以通过js调用退出当前应用的函数
class Api:
    def __init__(self) -> None:
        self._window = None

    def set_window(self, window):
        self._window = window

    def quit(self):
        self._window.destroy()

def init():
    pass

def py_disclaimer():
    return lib.helpers.disclaimer()

def material_getList():
    getlist = material.Material().getList()
    # jsonList = json.loads(getlimit)
    return getlist

def py_get_app_serial():
    return lib.helpers.get_app_serial()

def py_unAppcodekey(key):
    return lib.helpers.unAppcodekey(key)

def py_getFirstStart():
    first_start = material.Material().getTbVariables('first_start')
    if first_start['code']!=0:
        return {"code":-100,"msg":"系统出错！"}
    else:
        decryptStr = lib.helpers.decryptAppcodekey(first_start['requestdata'][3])
        return {"code":0,"reqdata":decryptStr}

#取配置表
def pymaterial_getBdConfigs():
    return material.Material().getBdConfigs()

#取变量表
def pymaterial_getTbVariables(key):
    return material.Material().getTbVariables(key)

def pymaterial_updateTbVariables(data):
    return material.Material().updateTbVariables(data)

def pymaterial_updateRec(data):
    return material.Material().updateRec(data)

def pymaterial_deleteRec(data):
    return material.Material().delete_id(data)

def pyDelete_all_data():
    return material.Material().delete_all_data()

def pyImportLins_data(data):
    return material.Material().importLins_data(data)

def pyUpdateIsuerRec(data):
    return material.Material().updateIsuerRec(data)

def pyDelete_ids(ids):
    return material.Material().delete_ids(ids)

def pyAddRec(data):
    return material.Material().addRec(data)

def pyWatchdog():
    return watchlogs(config.loggingfile)

def pyWatchClear():
    return writeNull(config.loggingfile)

def pyFeedBack(feeback):
    return sendEmail(feeback)

def pyUpdateIsuerAllRec(ids):
    return material.Material().updateIsuerAllRec(ids)

def pybdSetconfCache(opt='r',data=None):
    return helpers.bdSetconfCache(opt, data)

# def pyAppcdkeyHandle(opt=''):
#     return helpers.get_app_serial(opt)


def pybdStart(loop_option):
    ret = spiderbd_v3.BDkp().sync_main(loop_option)
    return ret



if __name__ == "__main__":
    # 实例化Api类
    init()
    api = Api()
    if not hasattr(webview, 'api'):
        window = webview.create_window(
            title='EsonSeo',
            fullscreen=False,  # 以全屏模式启动
            width=1180,  # 自定义窗口大小
            height=800,
            resizable=TRUE,  # 固定窗口大小
            text_select=False,  # 禁止选择文字内容
            confirm_close=True,  # 关闭时提示
            background_color='#000',
            js_api=api,  # 将上面实例化后的Api对象传给前端js调用
            #url='http://localhost:5173'
            url='index.html'
        )
        # --划重点--务必记得需要将上面创建的window对象再通过函数传给实例化后的api对象
        api.set_window(window)
        # 启动程序
        window.expose(material_getList, pymaterial_updateRec,pymaterial_deleteRec,pyDelete_all_data,pyImportLins_data,pyUpdateIsuerRec,pyDelete_ids,pyAddRec,pyWatchdog,pyWatchClear,pyFeedBack,pyUpdateIsuerAllRec,pybdSetconfCache,pybdStart,pymaterial_getBdConfigs,pymaterial_getTbVariables,pymaterial_updateTbVariables,py_get_app_serial,py_unAppcodekey,py_disclaimer,py_getFirstStart)  # 允许html 页面里访问test
        webview.start(localization=chinese, debug=False)
        # webview.start(localization=chinese)
    else:
        print("93 >>>>")

